
$(".deletecaidan").click(function(){
	var caidanid = $(this).attr("data-id");
	$.fn.confirm({
		msg:'确定删除这个菜单？',
		callback:function(){
			$.ajax({
				type:"post",
				url:"/uajax/getDelcaidan",
				data:"caidanid="+caidanid,
				dataType:"json",
				async: false,
				success: function(msg)
				{
					if(msg.data.flag == "Success")
					{
						window.location.href = "/?caipu/caidan";
					}else
					{
						showerrorinfo('提示',"删除失败！");
					}
				}
			})
		}
	})
})

var $cm_id		= $("#page_cm_id").attr('val');
$cm_type = $("#page_cm_id").attr('ctype');
var comment_responseTPL	= Tempo.prepare('comment_container');
var comment_totalTPL	= Tempo.prepare('comment_total');
showComments();
function showComments()
{
	$.getJSON('/?caipu/ajaxCommentsList/' + $cm_type + '/' + $cm_id + '/0/5/Desc', function (result){
			comment_responseTPL.starting();
			comment_responseTPL.render(result.data);
			comment_totalTPL.starting();
			comment_totalTPL.render(result.data);
	});
}

function submitComment(uid, comm)
{
	var comm	= $(comm).parent().find('textarea');
	var content	= $.trim(comm.val());
	if (content == '' || content.length <= 1)
	{
		showerrorinfo('提示信息', '评论内容不能为空！');
		return false;
	}
	if (!uid || uid <= 0)
	{
		logindialog();
		return false;
	}
	$('#commentbut').attr('disabled',"true");
	// init
	var cm_pid	= parseInt(comm.parent().find("input[name=cm_pid]").val());
	var cm_touid	= parseInt(comm.parent().find("input[name=cm_touid]").val());
	var cm_tounick	= comm.parent().find("input[name=cm_tounick]").val();
	var cm_touname	= comm.parent().find("input[name=cm_touname]").val();
	params	= '&pid=' + cm_pid + '&touser=' + cm_touid + '&nick=' + cm_tounick + '&name=' + cm_touname + '&type=' + $cm_type + '&id=' + $cm_id + '&comment=' + content;
	$(comm).parent().find('textarea').val(" ");
	$.ajax({
		type : 'post',
		url : '/?uajax/ajaxaddComment',
		data : params,
		dataType : 'json',
		success : function(msg) 
		{
			if (msg.status == "OK")
			{
				clearComment();
				showComments();
				$('#commentbut').removeAttr("disabled");
			}
			else
			{
				$('#commentbut').removeAttr("disabled");
				showerrorinfo('提示信息', '评论失败！');
			}
		}
	})
	
}
function reply(nick, uname, uid, cid)
{
	var comm_con	= $("#comment_container");
	var comm	= comm_con.find('textarea');
	comm.focus();
	comm.val('@' + nick + ':');
	comm.parent().find("input[name=cm_pid]").val(cid);
	comm.parent().find("input[name=cm_touid]").val(uid);
	comm.parent().find("input[name=cm_tounick]").val(nick);
	comm.parent().find("input[name=cm_touname]").val(uname);
	
}
function clearComment()
{
	var comm_con	= $("#comment_container");
	var comm	= comm_con.find('textarea');
	comm.focus();
	comm.val('');
	comm.parent().find("input[name=cm_pid]").val(0);
	comm.parent().find("input[name=cm_touid]").val(0);
	comm.parent().find("input[name=cm_tounick]").val('');
	comm.parent().find("input[name=cm_touname]").val('');
}

function caidanFavorites(self,caidan_id)
{
	var bool 	= $(self).hasClass('btncdsc') == true ? 0 : 1; //0：未收藏，1：收藏
	if(caidan_id)
	{
		$.ajax({
			type:'post',
			url:'/uajax/addDelCaidanfav',
			data:'&caidan_id='+caidan_id+"&status="+bool,
			dataType:'json',
			success:function(msg)
			{
				if(msg.data == 'LOGIN')
				{
					logindialog();
				}
				else if(msg.data == 'HAS')
				{
					showerrorinfo('错误提示', '此菜单已收藏！');
				}
				else if(msg.data == 'EMPTYINFO')
				{
					showerrorinfo('错误提示', '菜单不存在！');
				}
				else
				{
					if (bool)
					{
						$(self).find('a').text('添加收藏');
					}
					else
					{
						$(self).removeClass('btncdqxsc').addClass('btncdsc');
						$(self).find('a').text('取消收藏');
					}
				}
			}
		})
	}
}

function delcomment(commentid,type)
{
	if (commentid == '' || type == '')
	{
		return false;
	}
	params	= '&commentid=' + commentid + '&type=' + type;
	$.fn.confirm({
		msg:'确定删除这条评论信息？',
		callback:function(){
			$.ajax({
				type:"post",
				url:"/uajax/delCommend",
				data:params,
				dataType:"json",
				async: false,
				success: function(msg)
				{
					if(msg.data.info == 'NoLogin')
					{
						logindialog();
					}else if(msg.data.info == 'OK')
					{
						$('#'+commentid).remove();
						$('#caidantotalcomment').text('所有'+msg.data.tot+'条评论');
						//window.location.reload();
					}else
					{
						showerrorinfo('操作错误', '删除失败，请重试！');
					}
				}
			})
		}
	})
}